require 'tsort'

module Dior
  class Registry
    include TSort

    def initialize
      @components = {}
    end

    def add(component)
      @components[component.name] = component
    end
    alias << add

    def tsort_each_node(&block)
      @components.keys.each(&block)
    end

    def tsort_each_child(name, &block)
      @components[name].deps.each(&block)
    end

    def each
      tsort.each do |name|
        yield @components[name]
      end
    end
  end
end
